<?php
function attachmentSave($post=[])
{
    $post = DcArrayArgs($post,[
        'info_module'   => 'attachment',
        'info_controll' => 'detail',
        'info_action'   => 'index',
        'info_staus'    => 'normal',
        'info_type'     => '',//后缀
        'info_user_id'  => 1,
    ]);
    
    config('common.validate_name','attachment/Info');
        
    config('common.validate_scene','save');

    config('common.where_slug_unique',false);
    
    config('custom_fields.info_meta',model('common/Info','loglic')->metaKeys('attachment','detail'));

    return \daicuo\Info::save($post, 'info_meta');
}


function attachmentDelete($ids=[])
{
    //上传目录
    $uploadDir = './'.trim(config('common.upload_path'),'/').'/';
    //附件列表
    $files = db('info')->field('info_id,info_excerpt,info_mime_type')->where(['info_id'=>['in',$ids]])->select();
    //删除附件
    foreach($files as $key=>$value){
        \daicuo\Upload::delete($value['info_excerpt'],$value['info_mime_type']);
    }
    //删除记录
    return model('common/Info','loglic')->deleteIds($ids);
}


function attachmentUpdate($post=[])
{
    $post = DcArrayArgs($post,[
        'info_module'   => 'attachment',
        'info_controll' => 'detail',
        'info_action'   => 'index',
        'info_staus'    => 'normal',
        'info_type'     => '',
        'info_user_id'  => 1,
    ]);
    
    config('common.validate_name','attachment/Info');
        
    config('common.validate_scene','save');

    config('common.where_slug_unique',false);
    
    config('custom_fields.info_meta', model('common/Info','loglic')->metaKeys('attachment','detail'));
    
    return \daicuo\Info::update_id($post['info_id'], $post, 'info_meta');
}

function attachmentSelect($args)
{
    $args = DcArrayArgs($args,[
        'cache'    => true,
        'result'   => 'array',
        'module'   => 'attachment',
        'with'     => '',
        'view'     => [],
        'field'    => 'info_name,info_slug,info_title,info_excerpt,info_order,info_type,info_mime_type,info_status,info_views,info_hits,info_module,info_controll,info_action,info_create_time,info_user_id,info_update_time,info_keywords,info_description',
    ]);
    //动态生成视图查询或关联查询条件
    if($args['meta_query']){
        if(config('database.type') == 'mysql'){
            $args['field'].=',info.info_id';
        }else{
            $args['field'] = 'info.*';
        }
    }else{
        if($args['meta_key'] || $args['meta_value']){
            array_push($args['view'],['info_meta', NULL, 'info_meta.info_id=info.info_id']);
        }
        if($args['view']){
            $args['field'].=',info.info_id';
            array_push($args['view'],['info', NULL]);
        }else{
            $args['field'].=',info_id';
        }
    }
    //查询数据
    return model('common/Info','loglic')->select($args);
}

function attachmentGet($args)
{
    $args = DcArrayArgs($args,[
        'cache'    => true,
        'module'   => 'attachment',
    ]);
    
    return model('common/Info','loglic')->get($args);
}


function attachmentMetaQuery($query=[])
{
    return DcMetaQuery(model('common/Info','loglic')->metaList('attachment','detail'), $query);
}